package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.client.vo.OrderCustomReturn;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 非两票订单明细控制器
 *
 * @author flyme
 * @date 2022-11-15
 */
@RestController
@RequestMapping("/hy/bhd/")
public class HyOrderDetailController extends BaseController<HyOrderDetailService, HyOrderDetail> {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private SysCompanyService companyService;

    @ApiOperation(value = "非两票订单明细-分页列表", notes = "非两票订单明细分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "非两票订单明细-列表", notes = "非两票订单明细列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "非两票订单明细-添加", notes = "添加非两票订单明细")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "非两票订单明细-更新", notes = "更新非两票订单明细")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "非两票订单明细-更新", notes = "更新非两票订单明细")
    @PostMapping(value = "customUpdate")
    public ResultBody customUpdate(HyOrderDetail orderDetail) {
        bizService.customUpdate(orderDetail);
        return ResultBody.msg("核算成功");
    }

    @ApiOperation(value = "非两票订单明细-删除", notes = "删除非两票订单明细")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "非两票订单明细-详情", notes = "非两票订单明细详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "订单回款明细-列表", notes = "订单回款明细列表")
    @GetMapping(value = "returnList")
    public ResultBody returnList(@RequestParam(required = false) Long orderId) {
        List<EntityMap> list = paymentService.getHyOrderReturnDetails(orderId);
        return ResultBody.ok(list);
    }


    @ApiOperation(value = "拆分订单", notes = "拆分订单")
    @PostMapping(value = "chaiFen")
    public ResultBody chaiFen(OrderCustomReturn orderCustomReturn) {
        return bizService.chaiFen(orderCustomReturn);
    }

    @ApiOperation(value = "票折", notes = "票折")
    @PostMapping(value = "discount")
    public ResultBody discount(@RequestParam String orderDetailsId, @RequestParam BigDecimal saleAmount) {
        return bizService.addDiscountDetails(orderDetailsId, saleAmount);
    }

    @ApiOperation(value = "撤销拆分", notes = "撤销拆分")
    @PostMapping(value = "cancelChaiFen")
    public ResultBody cancelChaiFen(String orderDetailsId) {
        return bizService.cancelChaiFen(orderDetailsId);
    }


    @ApiOperation(value = "申请补货", notes = "申请补货")
    @PostMapping(value = "replenishOrder")
    public ResultBody replenishOrder(@RequestParam Long orderDetailsId, @RequestParam String saleDate, @RequestParam BigDecimal saleNum, @RequestParam(defaultValue = "CHC") String saleType) {
        return bizService.replenishOrder(orderDetailsId, saleType, saleNum, saleDate);
    }

    @ApiOperation(value = "非两票订单明细-核算", notes = "非两票订单明细-核算")
    @PostMapping(value = "calculate")
    public ResultBody calculate(@RequestParam(required = false) Map params) {
        return bizService.calculate(params);
    }

    @ApiOperation(value = "删除订单明细", notes = "删除订单明细")
    @PostMapping(value = "deleteById")
    public ResultBody deleteById(String orderDetailsId) {
        bizService.deleteById(orderDetailsId);
        return ResultBody.ok("删除成功");
    }

    @ApiOperation(value = "非两票订单订单- 统计", notes = "非两票订单年统计")
    @GetMapping(value = "reportYearHead")
    public ResultBody reportYearHead(@RequestParam(required = false) Map params) {
        return bizService.reportYearHead(params);
    }

    @ApiOperation(value = "非两票订单订单- 统计", notes = "非两票订单年统计")
    @GetMapping(value = "reportYearList")
    public ResultBody reportYearList(@RequestParam(required = false) Map params) {
        return bizService.reportYearList(params);
    }

    @ApiOperation(value = "非两票订单订单- 统计", notes = "非两票订单日统计")
    @GetMapping(value = "reportDayHead")
    public ResultBody reportDayHead(@RequestParam(required = false) Map params) {
        return bizService.reportDayHead(params);
    }

    @ApiOperation(value = "非两票订单订单- 统计", notes = "非两票订单日统计")
    @GetMapping(value = "reportDayList")
    public ResultBody reportDayList(@RequestParam(required = false) Map params) {
        return bizService.reportDayList(params);
    }


    @PostMapping(value = "updateOutPrintTag")
    public ResultBody updateOutPrintTag(@RequestParam String orderId) {
        bizService.updateOutPrintTag(orderId);
        return ResultBody.msg("打印成功");
    }

    @PostMapping(value = "updatePrintDrugJs")
    public ResultBody updatePrintDrugJs(@RequestParam String orderDetailsId, @RequestParam BigDecimal printDrugJs) {
        bizService.updatePrintDrugJs(orderDetailsId, printDrugJs);
        return ResultBody.msg("更新成功");
    }

    @ApiOperation(value = "获取商品标签印信息", notes = "获取商品标签印信息")
    @PostMapping(value = "getOutOrderPrint")
    public ResultBody getOutOrderPrint(String orderDetailsId, Integer customCount) {
        return bizService.getOutOrderPrint(orderDetailsId, customCount);
    }

    @ApiOperation(value = "获取商品标签印信息", notes = "获取商品标签印信息")
    @PostMapping(value = "getOutOrderPrint2")
    public ResultBody getOutOrderPrint2(String orderId) {
        return bizService.getOutOrderPrint2(orderId);
    }


    @ApiOperation(value = "获取出库单合并打印信息", notes = "获取出库单合并打印信息")
    @PostMapping(value = "getMergeOutOrderPrint")
    public ResultBody getMergeOutOrderPrint(String orderId) {
        return bizService.getMergeOutOrderPrint(orderId);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String companyName = OpenHelper.getAttr("companyName", String.class);
        String handlerName = MapUtil.getStr(params, "handlerName");
        String fileName = MapUtil.getStr(params, "fileName");
        bizService.export(params, request, response, companyName + "-" + fileName, fileName, handlerName);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "exportTemplate")
    public void exportTemplate(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String fileName = company.getCompanyName() + "-" + MapUtil.getStr(params, "fileName");
        bizService.templateExport(params, request, response, fileName + DateUtil.formatDate(DateUtil.date()), fileName, handlerName);
    }
}
